<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {height:34px;line-height:34px;padding:0 8px;}
</style>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div >
            <table id="table" class="layui-table" lay-filter="table" lay-size="lg"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-normal layui-btn-sm" href="javascription:;" layuimini-href="model-fields/create">添加字段</a>
    </div>
</script>
<!-- 分类列 -->
<script type="text/html" id="model_name">
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="model_name">@{{ d.model_name }}</a>
</script>
<!-- 状态列 -->
<script type="text/html" id="is_show">
    <input type="checkbox" name="is_show" value="@{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="is_show" @{{ d.is_show == 1 ? 'checked' : '' }}>
</script>
<!-- Is Title -->
<script type="text/html" id="is_table_title">
    <input type="checkbox" name="is_table_title" value="@{{d.id}}" lay-skin="switch" lay-text="是|否"
           lay-filter="is_table_title" @{{ d.is_table_title == 1 ? 'checked' : '' }}>
</script>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" layuimini-href="model-fields/@{{ d.id }}/edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'treetable','miniIndex'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var miniIndex = layui.miniIndex;

        // 渲染表格
        layer.load(2);
        var list = table.render({
            elem: '#table',
            url: 'model-fields/list',
            toolbar: '#toolbarDemo',
            title: '模型数据表',
            page: true,
            limit: 15,
            cols: [[
                {type: 'numbers'},
                {field: 'model_name', title: '模型',templet: '#model_name',align: 'center'},
                {field: 'name', title: '字段名称'},
                {field: 'type', title: '字段类型'},
                {field: 'form_name', title: '表单名称'},
                {field: 'form_type', title: '表单类型'},
                {field: 'is_table_title', title: '作为表格Title',templet: '#is_table_title',align: 'center'},
                {templet: '#is_show', title: '显示'},
                {field: 'order', title: '排序'},
                {field: 'created_at', title: '添加时间'},
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        // 切换推荐状态
        miniIndex.tableSwitch(list, 'model_field' , 'is_show');
        miniIndex.tableSwitch(list, 'model_field' , 'is_table_title');
        // 表格行操作事件
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            // 选取分类下产品
            if (layEvent === 'model_name') {
                list.reload({
                    where:{model_id: data.model_id}
                })
            } else if (layEvent === 'del'){
                layer.confirm('真的删除这个吗', function (index) {
                    layer.load(2);
                    $.ajax({
                        url: 'model-fields/' + data.id,
                        method: 'DELETE',
                        success: function (res) {
                            layer.msg(res.msg,function () {
                                obj.del();
                                layer.closeAll('loading');
                                layer.close(index);
                            })
                        }
                    })
                });
            }
        });
    });
</script>
</body>
